package br.com.ac.fatureweb.utilitary.converter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "telefoneConverter")
public class TelefoneConverter implements Converter
{

	/*
	 * (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(final FacesContext ctx, final UIComponent comp,
			final String value)
	{
		if ( value != null && !value.isEmpty() )
		{
			return replaceChar(value);
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(final FacesContext ctx, final UIComponent comp,
			final Object value)
	{
		if ( value != null )
		{
			final StringBuilder sb = formatarRetorno(value);

			return sb.toString();
		}

		return null;
	}

	private static StringBuilder formatarRetorno(final Object value)
	{
		final StringBuilder sb = new StringBuilder();

		/*
		 * Para número de telefone sem DDD
		 */
		if ( value.toString().length() == 8 )
		{
			final String tmp = value.toString();
			sb.append(tmp.substring(0, 4));
			sb.append('-');
			sb.append(tmp.substring(4, 8));
		}

		/*
		 * Para número de telefone com DDD + 8 dígitos
		 */
		else if ( value.toString().length() == 10 )
		{
			final String tmp = value.toString();
			sb.append("(");
			sb.append(tmp.substring(0, 2));
			sb.append(") ");
			sb.append(tmp.substring(2, 6));
			sb.append('-');
			sb.append(tmp.substring(6, 10));
		}

		/*
		 * Para número de telefone com DDD + 9 dígitos
		 */
		else if ( value.toString().length() == 11 )
		{
			final String tmp = value.toString();
			sb.append("(");
			sb.append(tmp.substring(0, 2));
			sb.append(") ");
			sb.append(tmp.substring(2, 7));
			sb.append('-');
			sb.append(tmp.substring(7, 11));
		}

		return sb;
	}

	private static String replaceChar(final String text)
	{
		final String[] REPLACES = { "", "", "", "" };

		Pattern[] PATTERNS;
		{
			PATTERNS = new Pattern[REPLACES.length];
			PATTERNS[0] = Pattern.compile("[(]", Pattern.CASE_INSENSITIVE);
			PATTERNS[1] = Pattern.compile("[)]", Pattern.CASE_INSENSITIVE);
			PATTERNS[2] = Pattern.compile("[-]", Pattern.CASE_INSENSITIVE);
			PATTERNS[3] = Pattern.compile("[ ]", Pattern.CASE_INSENSITIVE);
		}

		if ( text != null && text.trim().length() > 0 )
		{
			String result = text;

			for (int i = 0; i < PATTERNS.length; i++)
			{
				final Matcher matcher = PATTERNS[i].matcher(result);

				final String charParaReplace = REPLACES[i];

				result = matcher.replaceAll(charParaReplace);
			}

			return result;
		}

		return null;
	}
}